define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/index' + location.search,
                    add_url: 'shop/add',
                    edit_url: 'shop/edit',
                    del_url: 'shop/del',
                    multi_url: 'shop/multi',
                    table: 'shop',
                },
                showExport:false
            });

            var table = $("#table");
            table.on('load-success.bs.table', function (e, data) {
                var countData = data.extend.count;
                $('#tj').remove();
                var html = '<div id="tj" class="row" style="padding-top: 10px">';
                $.each(countData,function (index,value) {
                    html = html +  '<div class="table-dashboard-item-container form-group col-xs-12 col-sm-6 col-md-3 col-lg-3"><div class="table-dashboard-item-tip"><span class="table-dashboard-item-dot"></span>'+value.name+':</div>'+value.value+'</div>';
                });
                html = html+'</div>'
                $(".fixed-table-body").append(html);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                visible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                searchFormVisible:true,
                showExport: true,
                exportTypes: ['excel'],
                exportOptions: {
                    ignoreColumn: [0, 'operate'],
                    fileName: '店铺_'+ Moment().format("YYYY-MM-DD"),
                    onBeforeSaveToFile: function (data, fileName, type, charset, encoding, bom) {
                        return $.fn.bootstrapTable.defaults.extend.savestatus;
                    },
                    onCellHtmlHyperlink: function ($cell, rowIndex, colIndex, href, htmlData) {
                        var html = $.parseHTML(htmlData);
                        var inputidx = 0;
                        var selectidx = 0;
                        var result = '';

                        $.each(html, function () {
                            if ($(this).is("input")) {
                                result += $cell.find('input').eq(inputidx++).val();
                            } else if ($(this).is("select")) {
                                result += $cell.find('select option:selected').eq(selectidx++).text();
                            } else if ($(this).is("img")) {
                                var src = $(this).attr('src');
                                if (src.indexOf('http') === -1) {
                                    $(this).attr('src', window.location.origin + src);
                                }
                                $(this).attr("width", "40").attr("height", "40").attr('class', '');
                                result += '<div>' + $(this).get(0).outerHTML + '</div>';
                            } else {
                                if (typeof $(this).html() === 'undefined') {
                                    result += $(this).text();
                                } else if (jQuery().bootstrapTable === undefined ||
                                    ($(this).hasClass('filterControl') !== true &&
                                        $cell.parents('.detail-view').length === 0)) {
                                    result += $(this).html();
                                }
                            }
                        });
                        return result;
                    }
                },
                columns: [
                    [
                        // {field: 'districts', title: __('省、市、县区'), searchList: function (column) {
                        //         return Template('sourcetpl', {});
                        //     }, formatter: function (value, row, index) {
                        //         return '无';
                        //     }, visible: false
                        // },
                        {field: 'id', title: '商家ID',sortable: true,operate:false},
                        {field: 'shop_name', title: '商家名称', operate: 'LIKE'},
                        {field: 'agent.nickname', title: '所属代理商',operate: 'LIKE'},
                        {field: 'audit_status', title: '平台审核',formatter: Table.api.formatter.normal,searchList: {0: '待审核', 1: '审核通过',2:'审核失败'}},
                        {field: 'amap_province.name', title: __('Province'),operate: false},
                        {field: 'amap_city.name', title: __('City'),operate: false},
                        {field: 'amap_districts.name', title: __('Districts'),operate: false},
                        {field: 'address', title: __('Address'), operate: false},
                        {field: 'contacts', title: __('Contacts'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'wechat', title: '微信号', operate: false},
                        {field: 'legal_person', title: __('Legal_person'), operate: 'LIKE'},
                        {field: 'money', title: __('Money'), operate:false,sortable: true},
                        {field: 'all_money', title: '累计充值金额', operate:false,sortable: true},
                        {field: 'fixed_amount', title: __('Fixed_amount'),sortable: true, operate:false},
                        {field: 'invite_qrcode', title: '邀请码', formatter: Table.api.formatter.image, events: Table.api.events.image, operate: false},
                        {field: 'createtime', title: __('Createtime'),sortable: true, operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: '店铺状态', formatter: Table.api.formatter.status ,searchList: {1: '开启', 0: '关闭'}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: '审核',
                                    text: '审核',
                                    title: '审核',
                                    classname: 'btn btn-xs btn-danger btn-magic btn-dialog',
                                    url: 'shop/audit',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.audit_status == 0 || row.audit_status == 2) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    },
                                    callback: function (data) {
                                        $(".btn-refresh").trigger("click"); //刷新数据
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'shop/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'shop/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'shop/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        audit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on('click', "input[name='row[audit_status]']", function (e, ee) {
                    var dateType = $(this).val();
                    if (dateType == 2) {
                        $("#audit_reason").show();
                        $('#c-goods_detail').attr('data-rule',"required");
                    } else {
                        $("#audit_reason").hide();
                        $('#c-goods_detail').attr('data-rule',false);
                    }
                });
                $("input[name='row[audit_status]']:checked").trigger("click");
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
